//
//  UIButtonAdditions.m
//  Tint
//
//  Created by Kevin Wu on 4/18/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "UIButtonAdditions.h"


@implementation UIButton (Tint)


#pragma mark - Visible property

- (NSString *)normalTitle {
    return [self titleForState:UIControlStateNormal];
}
- (void)setNormalTitle:(NSString *)normalTitle {
    [self setTitle:normalTitle forState:UIControlStateNormal];
}

- (NSString *)hilightedTitle {
    return [self titleForState:UIControlStateHighlighted];
}
- (void)setHilightedTitle:(NSString *)hilightedTitle {
    [self setTitle:hilightedTitle forState:UIControlStateHighlighted];
}

- (NSString *)disabledTitle {
    return [self titleForState:UIControlStateDisabled];
}
- (void)setDisabledTitle:(NSString *)disabledTitle {
    [self setTitle:disabledTitle forState:UIControlStateDisabled];
}

- (NSString *)selectedTitle {
    return [self titleForState:UIControlStateSelected];
}
- (void)setSelectedTitle:(NSString *)selectedTitle {
    [self setTitle:selectedTitle forState:UIControlStateSelected];
}



- (UIColor *)normalTitleColor {
    return [self titleColorForState:UIControlStateNormal];
}
- (void)setNormalTitleColor:(UIColor *)normalTitleColor {
    [self setTitleColor:normalTitleColor forState:UIControlStateNormal];
}

- (UIColor *)hilightedTitleColor {
    return [self titleColorForState:UIControlStateHighlighted];
}
- (void)setHilightedTitleColor:(UIColor *)hilightedTitleColor {
    [self setTitleColor:hilightedTitleColor forState:UIControlStateHighlighted];
}

- (UIColor *)disabledTitleColor {
    return [self titleColorForState:UIControlStateDisabled];
}
- (void)setDisabledTitleColor:(UIColor *)disabledTitleColor {
    [self setTitleColor:disabledTitleColor forState:UIControlStateDisabled];
}

- (UIColor *)selectedTitleColor {
    return [self titleColorForState:UIControlStateSelected];
}
- (void)setSelectedTitleColor:(UIColor *)selectedTitleColor {
    [self setTitleColor:selectedTitleColor forState:UIControlStateSelected];
}



- (UIColor *)normalTitleShadowColor {
    return [self titleShadowColorForState:UIControlStateNormal];
}
- (void)setNormalTitleShadowColor:(UIColor *)normalTitleShadowColor {
    [self setTitleShadowColor:normalTitleShadowColor forState:UIControlStateNormal];
}

- (UIColor *)hilightedTitleShadowColor {
    return [self titleShadowColorForState:UIControlStateHighlighted];
}
- (void)setHilightedTitleShadowColor:(UIColor *)hilightedTitleShadowColor {
    [self setTitleShadowColor:hilightedTitleShadowColor forState:UIControlStateHighlighted];
}

- (UIColor *)disabledTitleShadowColor {
    return [self titleShadowColorForState:UIControlStateDisabled];
}
- (void)setDisabledTitleShadowColor:(UIColor *)disabledTitleShadowColor {
    [self setTitleShadowColor:disabledTitleShadowColor forState:UIControlStateDisabled];
}

- (UIColor *)selectedTitleShadowColor {
    return [self titleShadowColorForState:UIControlStateSelected];
}
- (void)setSelectedTitleShadowColor:(UIColor *)selectedTitleShadowColor {
    [self setTitleShadowColor:selectedTitleShadowColor forState:UIControlStateSelected];
}



- (UIImage *)normalImage {
    return [self imageForState:UIControlStateNormal];
}
- (void)setNormalImage:(UIImage *)normalImage {
    [self setImage:normalImage forState:UIControlStateNormal];
}

- (UIImage *)hilightedImage {
    return [self imageForState:UIControlStateHighlighted];
}
- (void)setHilightedImage:(UIImage *)hilightedImage {
    [self setImage:hilightedImage forState:UIControlStateHighlighted];
}

- (UIImage *)disabledImage {
    return [self imageForState:UIControlStateDisabled];
}
- (void)setDisabledImage:(UIImage *)disabledImage {
    [self setImage:disabledImage forState:UIControlStateDisabled];
}

- (UIImage *)selectedImage {
    return [self imageForState:UIControlStateSelected];
}
- (void)setSelectedImage:(UIImage *)selectedImage {
    [self setImage:selectedImage forState:UIControlStateSelected];
}



- (UIImage *)normalBackgroundImage {
    return [self backgroundImageForState:UIControlStateNormal];
}
- (void)setNormalBackgroundImage:(UIImage *)normalBackgroundImage {
    [self setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal];
}

- (UIImage *)hilightedBackgroundImage {
    return [self backgroundImageForState:UIControlStateHighlighted];
}
- (void)setHilightedBackgroundImage:(UIImage *)hilightedBackgroundImage {
    [self setBackgroundImage:hilightedBackgroundImage forState:UIControlStateHighlighted];
}

- (UIImage *)disabledBackgroundImage {
    return [self backgroundImageForState:UIControlStateDisabled];
}
- (void)setDisabledBackgroundImage:(UIImage *)disabledBackgroundImage {
    [self setBackgroundImage:disabledBackgroundImage forState:UIControlStateDisabled];
}

- (UIImage *)selectedBackgroundImage {
    return [self backgroundImageForState:UIControlStateSelected];
}
- (void)setSelectedBackgroundImage:(UIImage *)selectedBackgroundImage {
    [self setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected];
}



#pragma mark - Element alignment

- (void)centerButtonAndImageWithSpacing:(CGFloat)spacing {
    // Get the size of the elements here for readability
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    
    // Lower the text and push it left to center it
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);
    
    // The text width might have changed (in case it was shortened before due to lack of space and isn't anymore now), so we get the frame size again
    titleSize = self.titleLabel.frame.size;
    
    // Raise the image and push it right to center it
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0 - (titleSize.height + spacing), 0.0, 0.0, 0.0 - titleSize.width);
}

@end
